package com.fengye.middleware.rabbitmq.boot.messageType.topic;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author sky
 * @date 2021年06月09日 1:21 下午
 */
@Component
public class TopicExchangeConfig {

    @Value("${mq.topic.queue.one.name:topic_queue_one}")
    private String topicQueueNameOne;

    @Value("${mq.topic.queue.two.name:topic_queue_two}")
    private String topicQueueNameTwo;

    @Value("${mq.topic.exchange.name:topic_change_name}")
    private String topicExchangeName;

    @Value("${mq.topic.routing.key.one.name:topic_routing_key_one_name}")
    private String topicRoutingKeyOneName;


    @Value("${mq.topic.routing.key.two.name:topic_routing_key_two_name}")
    private String topicRoutingKeyTwoName;


    /* 创建简单消息模型：队列、交换机和路由 */

    // 队列
    @Bean(name = "topicQueueOne")
    public Queue topicQueueOne() {
        return new Queue(topicQueueNameOne);
    }

    @Bean(name = "topicQueueTwo")
    public Queue topicQueueTwo() {
        return new Queue(topicQueueNameTwo);
    }

    @Bean(name = "topicExchange")
    public TopicExchange topicExchange() {
        return new TopicExchange(topicExchangeName, true, false);
    }


    /**
     * 将队列 1 绑定到交换机上
     *
     * @param topicQueueOne 队列 1
     * @param topicExchange 交换机
     * @return org.springframework.amqp.core.Binding 绑定
     */
    @Bean
    public Binding topicBindingOne(Queue topicQueueOne, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueueOne).to(topicExchange).with(topicRoutingKeyOneName);
    }

    /**
     * 将队列 2 绑定到交换机上
     *
     * @param topicQueueTwo 队列 2
     * @param topicExchange 交换机
     * @return org.springframework.amqp.core.Binding 绑定结果
     */
    @Bean
    public Binding topicBindingTwo(Queue topicQueueTwo, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueueTwo).to(topicExchange).with(topicRoutingKeyTwoName);
    }
}
